-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
**BREAKING** pygmt.grdcut: Refactor to store output in virtualfiles for grids #3115
base: main
Are you sure you want to change the base?
Conversation
Minimal xarray.DataArray output with data and coordinates, no metadata yet.
d8f1160
to
d0517e0
Compare
d0517e0
to
4ba67df
Compare
Special case `earth_day` to be loaded as GMT_IMAGE rather than GMT_GRID. Need to use `GMT_OUT|GMT_IS_REFERENCE` in virtualfile_out to avoid segfault, xref #3115.
Extra metadata from the _GMT_GRID_HEADER struct.
Reorder the dimensions to follow Channel, Height, Width (CHW) convention. Also added doctest checking output DataArray object and the image's x and y coordinates.
Get the registration and gtype info from the grid header and apply it to the GMT accessor attributes.
Trying to match some of the doctests in _GMT_GRID.
pygmt/clib/session.py
Outdated
# The logic here is because: an image can be read into a grid container, but a grid | ||
# can't be read into an image container. So, try to read the file as an image first. | ||
# If fails, try to read it as a grid. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out the logic here is not 100% correct.
- An image can be read into a grid container, but only the first band is read.
- A grid can be read into an image container, as long as another libgdal package (maybe
libgdal-netcdf
orlibgdal-hdf5
) is installed. xref: Add libgdal-hdf5 dependency conda-forge/gmt-feedstock#293
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- A grid can be read into an image container, as long as another libgdal package (maybe
libgdal-netcdf
orlibgdal-hdf5
) is installed. xref:
The first try-except check is to try reading into a GMT_IMAGE
struct, and return "image" if there are 3 bands, and "grid" if there is only 1 band. Even if grids can be read into GMT_IMAGE
, we will still call it a grid if it has 1-band right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to refresh my memory about what's happening:
- Reading a grid into
GMT_GRID
: Works! - Reading an image into
GMT_GRID
: Only the first band is read. - Reading a grid into
GMT_IMAGE
: It depends onlibgdal-hdf5
being installed. Read as an single-band image if installed, otherwise raise errors. - Reading an image into
GMT_IMAGE
: works.
import pygmt
pygmt.grdcut("@earth_relief_01d_p", region=[0, 10, 0, 10])
If libgdal-hdf5
is not installed, _raster_kind
first read it as an image, which fails and reports following errors:
ERROR 4: `/home/runner/.gmt/server/earth/earth_relief/earth_relief_01d_p.grd' not recognized as being in a supported file format. It could have been recognized by driver HDF5, but plugin gdal_HDF5.so is not available in your installation. You may install it with 'conda install -c conda-forge libgdal-hdf5'
Error: ession [ERROR]: Unable to open earth_relief_01d_p.grd.
Error: ession [ERROR]: ERROR reading image with gdalread.
pygmt-session (gmtapi_import_image): Could not read from file [earth_relief_01d_p.grd]
[Session pygmt-session (43)]: Error returned from GMT API: GMT_IMAGE_READ_ERROR (22)
[Session pygmt-session (43)]: Error returned from GMT API: GMT_IMAGE_READ_ERROR (22)
then it tries to read it as a grid, which succeeds. But we still see the error messages above. I believe we need to find a way to suppress these errors or find an alternative way to determine the data kind.
outkind = kind | ||
case "_": | ||
msg = f"Unsupported data type {type(grid)}." | ||
raise GMTInvalidInput(msg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 117-118 should be caught by the test_grdcut_fails
test.
This PR applies virtualfiles in
pygmt.grdcut
, so that this function doesn't have to write to temporary files (#2730).grdcut
is on of the complicated module that it works for grid, image and cube. We have to know thekind
of the input before calling theSession.virtualfile_out
method.It turns out it's not trivial to determine the data
kind
of an input file. We have some options:.nc
is a grid,.tif
is a image)kind
parameter (default to"grid"
) and users should specify thekind
parameter.Option 1-2 may not always work; option 3 also dosn't always work (see #3115 (comment)).
This PR currently adopts option 4. Need to note option 4 meaning BREAKING:
grdcut("@earth_day_01d")
works; now must usegrdcut("@earth_day_01d", kind="image")